#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include "RingQueue.hpp"

struct threaddata {
    RingQueue<int> *rq;
    string name;
};

void* consumer(void* args) {
    threaddata* td = static_cast<threaddata*>(args);
    while (1) {
        sleep(1);
        int t = 0;
        td->rq->Pop(&t);
        // 消费数据
        cout << td->name << "消费者拿到了一个数据：" << t << endl;
    }
}
int k = 0;
void* productor(void* args) {
    threaddata* td = static_cast<threaddata*>(args);
    while (1) {
        sleep(1);
        cout << td->name << "生产了一个任务：" << k << endl;
        td->rq->Equeue(k++);
    }
}

int main() {
    RingQueue<int>* rq = new RingQueue<int>();
    pthread_t c[2], p[3];
    threaddata* td = new threaddata();
    td->name = "cthread_1";
    td->rq = rq;
    pthread_create(c, nullptr, consumer, td);

    threaddata* td2 = new threaddata();
    td2->name = "cthread_2";
    td2->rq = rq;
    pthread_create(c + 1, nullptr, consumer, td2);

    threaddata* td3 = new threaddata();
    td3->name = "pthread_3";
    td3->rq = rq;
    pthread_create(p, nullptr, productor, td3);

    threaddata* td4 = new threaddata();
    td4->name = "pthread_4";
    td4->rq = rq;
    pthread_create(p + 1, nullptr, productor, td4);

    threaddata* td5 = new threaddata();
    td5->name = "pthread_5";
    td5->rq = rq;
    pthread_create(p + 2, nullptr, productor, td5);

    pthread_join(c[0], nullptr);
    pthread_join(c[1], nullptr);
    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);
    pthread_join(p[2], nullptr);
    return 0;
}